package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)

type Daily struct {
	XMLName xml.Name `xml:"rss"`
	Channel Channel  `xml:"channel"`
}

type Channel struct {
	Title       string `xml:"title"`
	Description string `xml:"description"`
	Link        string `xml:"link"`
	Ttl         int    `xml:"ttl"`
	Item        []Item `xml:"item"`
}

type Item struct {
	Title       string `xml:"title"`
	Description string `xml:"description"`
}

func main() {
	x := new(Daily)
	// 获取xml的response
	resp, err := http.Get("https://www.solidot.org/index.rss")
	if err != nil {
		fmt.Fprintf(os.Stderr, "GET Response fail:%v\n", resp)
		return
	}
	// 读取到[]byte
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Read body: %v", err)
		return
	}
	err = xml.Unmarshal(data, x)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Decode xml: %v", err)
		return
	}

	fmt.Println("Daily:XMLname", x.XMLName)
	fmt.Println("channel:")
	fmt.Println("chanel.title", x.Channel.Title)
	for _, item := range x.Channel.Item {
		fmt.Println("title: ", item.Title)
		fmt.Println("desc: ", item.Description)
	}

}
